package utils;

import irio.gamer.gamer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

public class IniFile {

	public HashMap<String, String> data = new HashMap<String, String>();

	private String content = "";

	public IniFile() {
		content = "";
	}
	
	public IniFile(String filename) throws GamerException {

		try {
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			String line;
			while ((line = reader.readLine()) != null) {
				//System.out.println("line:" + line);
				if (!line.startsWith("#")) {
					StringTokenizer st = new StringTokenizer(line, "=");
					if (st.countTokens()==2) {
						String key = st.nextToken().trim();
						String value = st.nextToken().trim();
						//System.out.println("Adding key : " + key);
						data.put(key, value);
					}
				}

			}
			reader.close();
		} catch (Exception e) {
			throw new GamerException("Error opening file: " + filename);
		}
	}

	public String getString(final String name) throws GamerException {
		try {
			String s =  data.get(name);
			if (s == null)
				throw new Exception();
			else return s;
		} catch (Exception e) {
			throw new GamerException("Could not find key '" + name +"'");
		}
	}

	public boolean getBoolean(final String name) {

		String s = data.get(name);
		if (s.equals("true"))
			return true;
		return false;
	}

	public double getDouble(final String name) throws GamerException {
		try {
		return Double.parseDouble(data.get(name));
		} catch (Exception e) {
			throw new GamerException("Could not find key '" + name +"'");
		}
	}
	public float getFloat(final String name) {
		return Float.parseFloat(data.get(name));
	}

	public Vec getVec(String name) throws GamerException {
		Vec res = new Vec();

		try {
		StringTokenizer st = new StringTokenizer(data.get(name), ",");

		res.x(Double.parseDouble(st.nextToken().trim()));
		res.y(Double.parseDouble(st.nextToken().trim()));
		res.z(Double.parseDouble(st.nextToken().trim()));

		} catch (Exception e) {
			throw new GamerException("Could not find key '" + name +"'");
		
		}
		return res;
	}

	
	
	public void clearContent() {
		content = "";
	}
	
	public void addKey(String name, Vec v) {
		content += name + " = " + v.x +", " +v.y +", " +v.z +" \n";  
	}

	public void addKey(String name, double d) {
		content += name + " = " + d + " \n";  
	}

	public void addKey(String name, int i) {
		content += name + " = " + i + " \n";  
	}
	public void addKey(String name, String k) {
		content += name + " = " + k + " \n";  
	}
	public void addSpace() {
		content += " \n";  
	}
	

	
	public void Save(String filename) {
		BufferedWriter writer = null;
		try
		{
			writer = new BufferedWriter( new FileWriter( filename ));
			writer.write( content);
			writer.close( );

		}
		catch ( IOException e)
	{
			gamer.HandleException(e, "Error writing to " + filename);
	}
	}

	public static void SaveTextfile(String filename, String cnt) {
		BufferedWriter writer = null;
		try
		{
			writer = new BufferedWriter( new FileWriter( filename ));
			writer.write( cnt );
			writer.close( );

		}
		catch ( IOException e)
	{
			gamer.HandleException(e, "Error writing to " + filename);
	}
	}

	public static void AddToTextfile(String filename, String cnt) {
		BufferedWriter writer = null;
		try
		{
			writer = new BufferedWriter( new FileWriter( filename, true ));
			writer.write( cnt );
			writer.close( );

		}
		catch ( IOException e)
	{
			gamer.HandleException(e, "Error writing to " + filename);
	}
	}

	
	
}
